package com.dw.oss.controller;

import com.dw.oss.dao.MetadataRepositoryImpl;
import com.dw.oss.entity.FileMetadataInfoEntity;
import com.dw.oss.utils.LoginUtil;
import org.liuxp.minioplus.api.StorageService;
import org.liuxp.minioplus.api.model.dto.FileMetadataInfoDTO;
import org.liuxp.minioplus.api.model.dto.FileMetadataInfoSaveDTO;
import org.liuxp.minioplus.api.model.vo.FileMetadataInfoVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@RestController
@RequestMapping("/oss")
public class FileMetadataInfoController {

    @Resource
    private MetadataRepositoryImpl metadataRepositoryImpl;

    @Resource
    private StorageService storageService;

    @PostMapping("/list")
    public List<FileMetadataInfoEntity> list() {
        FileMetadataInfoDTO fileMetadataInfoDTO = new FileMetadataInfoDTO();
        fileMetadataInfoDTO.setBucket("digital-wonderland");
        return metadataRepositoryImpl.list();
    }

    @GetMapping("/download")
    public String download(@RequestParam String fileKey,@RequestParam String userId) {
        return storageService.download(fileKey, userId);
    }

    @GetMapping("/image")
    public String image(@RequestParam String fileKey,@RequestParam String userId) {
        return storageService.image(fileKey, userId);
    }

    @GetMapping("/preview")
    public String preview(@RequestParam String fileKey,@RequestParam String userId) {
        return storageService.preview(fileKey, userId);
    }

    @DeleteMapping("/delete")
    public Boolean remove(@RequestParam String fileKey) {
        return storageService.remove(fileKey);
    }

    @GetMapping("/one")
    public FileMetadataInfoVo one(@RequestParam String key) {
        return storageService.one(key);
    }

    @PostMapping("/save")
    public FileMetadataInfoVo save(MultipartFile file) {
//        return metadataRepositoryImpl.save(saveDTO);
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return storageService.createFile(file.getOriginalFilename(), true, LoginUtil.getLoginId(), inputStream);
    }
}
